<template>
  <div>
    <el-card class="!border-none mb-4" shadow="never">
      <el-form class="mb-[-16px]" :model="queryParams" inline>

        <el-form-item label="时间(年月)" prop="bid">
          <el-select class="w-[280px]" v-model="queryParams.search_month" clearable placeholder="请选择时间(年月)">
            <el-option label="全部" value=""></el-option>
            <el-option
                v-for="(item, index) in searchLists.month_list"
                :key="index"
                :label="item.name"
                :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="院区/楼栋">
          <el-cascader
              :options="searchLists.cb_lists"
              clearable
              v-model="queryParams.cb_ids"
              filterable
              :props="props1"
          />
        </el-form-item>

        <el-form-item label="用户信息" prop="user_keyword">
          <el-input class="w-[280px]" v-model="queryParams.user_keyword" clearable placeholder="请输入用户名/手机号"/>
        </el-form-item>

        <el-form-item label="房间名" prop="room_keyword">
          <el-input class="w-[280px]" v-model="queryParams.room_keyword" clearable placeholder="请输入房间名/地址"/>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
          <el-button type="warning" @click="handlePrintUserStat">打印-住户汇总</el-button>
          <el-button type="warning" @click="handlePrintUserDevice">打印-住户详细</el-button>
          <export-data
              class="ml-2.5"
              :button-text="'导出-住户汇总'"
              :fetch-fun="apiRoomApartmentExportListsUserStat"
              :params="queryParams"
              :page-size="pager.size"/>
          <export-data
              class="ml-2.5"
              :button-text="'导出-住户详细'"
              :fetch-fun="apiRoomApartmentExportListsUserDevice"
              :params="queryParams"
              :page-size="pager.size"/>

          <!--<print-bill 注意：未完成 class="ml-2.5" :getListsUrl="apiRoomApartmentPrintListsUserStat" :queryParams="queryParams"/>-->

        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="!border-none" v-loading="pager.loading" shadow="never">
      <!--            <el-button v-perms="['room.room_apartment_user/add']" type="primary" @click="handleAdd">-->
      <!--                <template #icon>-->
      <!--                    <icon name="el-icon-Plus" />-->
      <!--                </template>-->
      <!--                新增-->
      <!--            </el-button>-->
      <!--            <el-button-->
      <!--                v-perms="['room.room_apartment_user/delete']"-->
      <!--                :disabled="!selectData.length"-->
      <!--                @click="handleDelete(selectData)"-->
      <!--            >-->
      <!--                删除-->
      <!--            </el-button>-->

      <div class="mt-4">
        <el-table :data="pager.lists" @selection-change="handleSelectionChange">

          <el-table-column type="expand" width="50">
            <template #default="{ row }">
              <el-table :data="row.allRoomStat">
                <el-table-column label="房间信息" prop="row.room" width="250">
                  <template #default="{ row }">
                    <view v-if="row.campus">{{ row.campus.name }} /</view>
                    <view v-if="row.building">{{ row.building.name }} /</view>
                    {{ row.name }}

                    <view v-if="row.log_date">
                      <view v-if="row.cname">{{ row.cname }} /</view>
                      <view v-if="row.bname">{{ row.bname }} /</view>
                      {{ row.name }}
                    </view>
                  </template>
                </el-table-column>
                <el-table-column label="人数" prop="user_count" width="100" show-overflow-tooltip/>
                <el-table-column label="电梯费" prop="room_lift_fee" show-overflow-tooltip/>
                <el-table-column label="二次加压费" prop="room_second_pressure_fee" show-overflow-tooltip/>
                <el-table-column label="用电量" prop="room_d_use_total" show-overflow-tooltip/>
                <el-table-column label="电费" prop="room_d_total_fee" show-overflow-tooltip/>
                <el-table-column label="用水量" prop="room_s_use_total" show-overflow-tooltip/>
                <el-table-column label="水费" prop="room_s_total_fee" show-overflow-tooltip/>
                <el-table-column label="总费用" prop="room_all_total_fee" show-overflow-tooltip/>
              </el-table>
            </template>
          </el-table-column>

          <el-table-column label="住户" prop="user_id" width="200" show-overflow-tooltip>
            <template #default="{ row }">
              {{ row.user_name }}<br/>{{ row.user_mobile }}
            </template>
          </el-table-column>
          <el-table-column label="房间数" prop="room_count" width="100" show-overflow-tooltip/>
          <el-table-column label="总电梯费" prop="all_lift_fee" show-overflow-tooltip/>
          <el-table-column label="总二次压力费" prop="all_second_pressure_fee" show-overflow-tooltip/>
          <el-table-column label="总用电量" prop="all_d_use_total" show-overflow-tooltip/>
          <el-table-column label="总电费" prop="all_d_total_fee" show-overflow-tooltip/>
          <el-table-column label="总用水量" prop="all_s_use_total" show-overflow-tooltip/>
          <el-table-column label="总水费" prop="all_s_total_fee" show-overflow-tooltip/>
          <el-table-column label="总费用" prop="all_total_fee" show-overflow-tooltip/>
        </el-table>
      </div>
      <div class="flex mt-4 justify-end">
        <pagination v-model="pager" @change="getLists"/>
      </div>
    </el-card>
    <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false"/>

    <print-popup v-if="showPrint" ref="printRef" @close="showPrint = false"
                 :print-keyword="printLists.pkl_user_device_keyword"
                 :print-lists="printLists.pkl_user_device_lists"/>

  </div>
</template>

<script lang="ts" setup name="roomApartmentUserLists">
import {usePaging} from '@/hooks/usePaging'
import {useDictData} from '@/hooks/useDictOptions'
import {
  apiRoomApartmentUserLists,
  apiRoomApartmentUserDelete,
  apiRoomApartmentPrintListsUserDevice,
  apiRoomApartmentPrintListsUserStat,
  apiRoomApartmentExportListsUserStat,
  apiRoomApartmentExportListsUserDevice,
} from '@/api/room/room_apartment_user'
import {timeFormat} from '@/utils/util'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import {View} from "@element-plus/icons-vue";
import {reactive} from "vue";
import {apartmentSearchLists} from "@/api/room/room_apartment";

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)

const props1 = {
  checkStrictly: true
}


import PrintPopup from '@/views/system/print_page.vue'

const printRef = shallowRef<InstanceType<typeof PrintPopup>>()
const showPrint = ref(false)
const handlePrint = async (data: any) => {
  showPrint.value = true
  await nextTick()
  printRef.value?.open()
}
const printLists = reactive({
  pkl_user_device_keyword: [],
  pkl_user_device_lists: [],
})
const handlePrintUserStat = async () => {
  const print = await apiRoomApartmentPrintListsUserStat(queryParams)
  printLists.pkl_user_device_keyword = print.keyword
  printLists.pkl_user_device_lists = print.lists
  showPrint.value = true
  await nextTick()
  printRef.value?.open()
}
const handlePrintUserDevice = async () => {
  const print = await apiRoomApartmentPrintListsUserDevice(queryParams)
  printLists.pkl_user_device_keyword = print.keyword
  printLists.pkl_user_device_lists = print.lists
  showPrint.value = true
  await nextTick()
  printRef.value?.open()
}


// 查询条件
const queryParams = reactive({
  cid: '',
  rid: '',
  add_time: '',
  status: '',
  user_id: '',
  start_time: '',
  end_time: '',
  cb_ids: '',
  search_month: '',
  user_keyword: '',
  room_keyword: '',
  device_keyword: '',
})

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
  selectData.value = val.map(({id}) => id)
}

// 获取字典数据
const {dictData} = useDictData('show_status')

const searchLists = reactive({
  cb_lists: [],
  month_list: [],
  pkl_user_device: [],
})
const getSearchLists = async () => {
  const lists = await apartmentSearchLists([])
  searchLists.cb_lists = lists.cb_lists
  searchLists.month_list = lists.month_list
  searchLists.pkl_user_device = lists.pkl_user_device
  return lists;
}
getSearchLists()

// 分页相关
const {pager, getLists, resetParams, resetPage} = usePaging({
  fetchFun: apiRoomApartmentUserLists,
  params: queryParams
})

// 添加
const handleAdd = async () => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
  showEdit.value = true
  await nextTick()
  editRef.value?.open('edit')
  editRef.value?.setFormData(data)
}

// 删除
const handleDelete = async (id: number | any[]) => {
  await feedback.confirm('确定要删除？')
  await apiRoomApartmentUserDelete({id})
  getLists()
}

getLists()
</script>

